package com.smartleanx.module.hrm.controller.app.employee;

import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import com.smartleanx.framework.security.core.annotations.PreAuthenticated;
import com.smartleanx.module.hrm.context.EmployeeContextHolder;
import com.smartleanx.module.hrm.controller.admin.employee.contract.EmployeeContractRepVo;
import com.smartleanx.module.hrm.controller.admin.employee.contract.EmployeeContractRespVO;
import com.smartleanx.module.hrm.controller.admin.employee.contract.EmployeeContractSaveReqVO;
import com.smartleanx.module.hrm.controller.admin.employee.contract.EmployeeContractSimpleRespVo;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeContractDO;
import com.smartleanx.module.hrm.service.employee.EmployeeContractService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.smartleanx.framework.common.pojo.CommonResult.success;

@Tag(name = "用户APP - 员工合同")
@RestController
@RequestMapping("/hrm/employee-contract")
@Validated
public class APPEmployeeContractController {

    @Resource
    private EmployeeContractService employeeContractService;

    @PostMapping("/create")
    @Operation(summary = "创建员工合同")
    @PreAuthenticated
    public CommonResult<Long> createEmployeeContract(@Valid @RequestBody EmployeeContractSaveReqVO createReqVO) {
        return success(employeeContractService.createEmployeeContract(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新员工合同")
    @PreAuthenticated
    public CommonResult<Boolean> updateEmployeeContract(@Valid @RequestBody EmployeeContractSaveReqVO updateReqVO) {
        employeeContractService.updateEmployeeContract(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除员工合同")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthenticated
    public CommonResult<Boolean> deleteEmployeeContract(@RequestParam("id") Long id) {
        employeeContractService.deleteEmployeeContract(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得员工合同")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<EmployeeContractRespVO> getEmployeeContract(@RequestParam("id") Long id) {
        return success(employeeContractService.getEmployeeContract(id));
    }

    @GetMapping("/getContractStatus")
    @Operation(summary = "获得员工合同执行状态")
    @PreAuthenticated
    public CommonResult<Integer> contractStatus(@Valid EmployeeContractRepVo repVo) {
        return success(employeeContractService.contractStatus(repVo));
    }

    @GetMapping("/list")
    @Operation(summary = "根据员工id获得员工合同列表")
    @PreAuthenticated
    public CommonResult<List<EmployeeContractRespVO>> getEmployeeContractList() {
        Long employeeId = EmployeeContextHolder.getEmployeeId();
        return success(employeeContractService.getEmployeeContractList(employeeId));
    }

}
